package com.mf.controller.admin;

import com.mf.entity.GoodsSpec;
import com.mf.entity.Log;
import com.mf.entity.SpecItem;
import com.mf.service.GoodsSpecService;
import com.mf.service.LogService;
import com.mf.service.SpecItemService;
import com.mf.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/goodsSpec")
public class GoodsSpecController {

    @Autowired
    private GoodsSpecService goodsSpecService;

    @Resource
    private LogService logService;
    @Autowired
    private SpecItemService specItemService;


    @RequestMapping("/list")
    public Map<String, Object> getGoodsSpecList(GoodsSpec goodsSpec, @RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "rows", required = false) Integer rows) throws Exception {
        Map<String, Object> result = new HashMap<>();
        List<GoodsSpec> goodsSpeList = goodsSpecService.list(goodsSpec, page, rows, Sort.Direction.ASC, "id");
        List<GoodsSpec> gs= new ArrayList<>();
        for (GoodsSpec g:goodsSpeList){
            List<SpecItem> specItemList = specItemService.findListBySpecId(g.getId());
            g.setSpecItemList(specItemList);
            gs.add(g);
        }

        Long goodsSpecCount = goodsSpecService.getCount(goodsSpec);
        result.put("rows", gs);
        result.put("total", goodsSpecCount);
        logService.save(new Log(Log.SEARCH_ACTION, "查询SKU商品规格信息"));
        return result;
    }


    @RequestMapping("/save")
    public Map<String, Object> save(GoodsSpec goodsSpec) throws Exception {
        Map<String, Object> result = new HashMap<>();

        if (goodsSpec.getId() != null) {
            logService.save(new Log(Log.UPDATE_ACTION, "更新sku商品规格" + goodsSpec));
            //修改规格项
            if (StringUtil.isNotEmpty(goodsSpec.getStrItem())) {
                List<SpecItem> specItemList = specItemService.findListBySpecId(goodsSpec.getId());
                for (SpecItem specItem : specItemList) {
                    specItemService.delete(specItem.getId());
                }
            }
        } else {
            logService.save(new Log(Log.ADD_ACTION, "添加sku商品规格" + goodsSpec));
        }


        goodsSpecService.save(goodsSpec);
       // GoodsSpec goodsSpecId  = goodsSpecService.getId(goodsSpec);
        //保存规格项
        saveSpecItem( goodsSpec );
        result.put("success", true);
        return result;
    }

    private void saveSpecItem(GoodsSpec goodsSpec ){
        if (StringUtil.isNotEmpty(goodsSpec.getStrItem())) {
            String[] strItem = goodsSpec.getStrItem().split("\\r\\n");
            for (String str : strItem) {
                SpecItem specItem = new SpecItem();
                specItem.setItem(str);
                specItem.setGoodsSpecId(goodsSpec.getId());
                specItemService.save(specItem);
            }

        }
    }

    @RequestMapping("/delete")
    public Map<String, Object> delete(String ids) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();

        String[] idsStr = ids.split(",");
        for (int i = 0; i < idsStr.length; i++) {
            int id = Integer.parseInt(idsStr[i]);
            logService.save(new Log(Log.UPDATE_ACTION, "删除sku商品规格 id:" + id));
            goodsSpecService.delete(id);
        }

        resultMap.put("success", true);
        return resultMap;
    }

}
